/*
'--------------------------------------
'Power By 方卡在线 @2010-2014 f02
'URL:http://www.fangka.net/
'EMail:admin@ifangka.com
'QQ:861118936
'版权所有 未经允许不得去除本版权信息
'--------------------------------------
*/

//页面载入完毕宽度重设
$(document).ready(function(){
	pageReset();
});

//监听浏览器大小变化
//$(window).resize(function() {
//    pageReset();
//});

//监听按键
$(document).keydown(function(event){ 
	if(event.keyCode==27){
		dialogClose();
	}
}); 


//页面宽度重设
function pageReset(){
	var leftWidth=189;
	var rightWidth=$(window).width()-189;
	var documentHeight=$(document).height()-90;
	var leftHeight=$("#mainLeft").height();
	var rightHeight=$("#mainRight").height();
	if(rightWidth>812){
		$("#mainRight").width(rightWidth);
		$("#allbox").width($(window).width());
	}else{
		$("#mainRight").width(812);
		$("#allbox").width(1001);
	}
	if(leftHeight<documentHeight||leftHeight<rightHeight){
		if(rightHeight>documentHeight){
			$("#mainLeft").height(rightHeight);
		}else{
			$("#mainLeft").height(documentHeight);
		}
	}
	$("#dialogAlpha").height($(document).height());
	$("#dialogAlpha").width($(document).width());
}

//管理员页面初始化
function adminLoading(){
	ajax_get('userInfo','Get.asp?Type=1');
	ajax_get('nav','Get.asp?Type=2');
	if(rUrl!=''){
		ajax_get('mainLeft',rUrl);
	}
	ajax_get('mainRight','Get.asp?Type=4');
}

//菜单项类型切换
function ModuleTypeChange(mType){
	$('#m_Model').hide();
	$('#m_Template').hide();
	$('#m_GBookReply').hide();
	$('.m_List').hide();
	$('.m_Url').hide();
	switch(mType){
		case '0':
			$('#m_Template').show();
			break;
		case '1':
			$('#m_Model').show();
			$('#m_Template').show();
			$('.m_List').show();
			ChangeSelect('Select.asp?Type=1&m=1','Fk_Module_Model');
			break;
		case '2':
			$('#m_Model').show();
			$('#m_Template').show();
			ChangeSelect('Select.asp?Type=1&m=2','Fk_Module_Model');
			break;
		case '3':
			$('#m_Model').show();
			$('#m_Template').show();
			$('#m_GBookReply').show();
			$('.m_List').show();
			$('#m_LowTemplate').hide();
			ChangeSelect('Select.asp?Type=1&m=3','Fk_Module_Model');
			break;
		case '4':
			$('.m_Url').show();
			break;
		case '5':
			break;
	}
}

//模型类型切换
function ModelTypeChange(mType){
	$('.m_View').hide();
	switch(mType){
		case '1':
			$('.m_View').show();
			$('#m_List').show();
			break;
		case '2':
			$('#m_List').hide();
			break;
	}
}

//管理员权限切换
function AdminTypeChange(mType){
	if(mType=='1'){
		$('.a_limit').show();
		$('.a_limitView').hide();
	}else{
		$('.a_limit').hide();
	}
}

//管理员权限切换
function AdminViewLimitChange(mType){
	if(mType=='1'){
		$('.a_limitView').hide();
	}else{
		$('.a_limitView').show();
	}
}

(function($){ $.fn.simpletooltip = function(){
	return this.each(function() {
		var text = $(this).attr("title");
		$(this).attr("title", "");
		if(text != undefined) {
			$(this).hover(function(e){
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				$(this).attr("title", ""); 
				$("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 105; display: none;'>" + text + "</div>");
				if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
				else var tipWidth = $("#simpleTooltip").width()
				$("#simpleTooltip").width(tipWidth);
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
			}, function(){
				$("#simpleTooltip").remove();
				$(this).attr("title", text);
			});
			$(this).mousemove(function(e){
				var tipX = e.pageX + 12;
				var tipY = e.pageY + 12;
				var tipWidth = $("#simpleTooltip").outerWidth(true);
				var tipHeight = $("#simpleTooltip").outerHeight(true);
				if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
				if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
				$("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
			});
		}
	});
}})(jQuery);

//File Upload
(function($){
	$.fn.upload = function(options) {
		/** Merge the users options with our defaults */
		options = $.extend({
			name: 'file',
			enctype: 'multipart/form-data',
			action: '',
			autoSubmit: true,
			callBackOption: {},
			onSubmit: function() {},
			onComplete: function() {},
			onSelect: function() {},
			params: {}
		}, options);

		return new $.ocupload(this, options);
	},
	$.ocupload = function(element, options) {
		/** Fix scope problems */
		var self = this;
		var IE = /msie ((\d+\.)+\d+)/i.test(navigator.userAgent) ? (document.documentMode ||  RegExp['\x241']) : false;
	
		/** A unique id so we can find our elements later */
		var id = new Date().getTime().toString().substr(8);
		
		/** Upload Iframe */
		var iframe = $(
			'<iframe '+
				'id="iframe'+id+'" '+
				'class="oc_upload" '+
				'name="iframe'+id+'"'+
			'></iframe>'
		).css({
			display: 'none'
		});
		
		/** Form */
		var formid='form'+id;
		var form = $(
			'<form '+
				'method="post" '+
				'class="oc_upload" '+
				'id="'+formid+'" '+
				'enctype="'+options.enctype+'" '+
				'action="'+options.action+'" '+
				'target="iframe'+id+'"'+
			'></form>'
		).css({
			margin: 0,
			padding: 0
		});
		
		/** File Input */
		var inputid='input'+id;
		var input = $(
			'<input '+
				'class="oc_upload" '+
				'name="'+options.name+'" '+
				'id="'+inputid+'" '+
				'type="file" '+
			' style="z-index:999;" />'
		).css({
			position: 'absolute',
			cursor: 'pointer',
			title: '',
			top: 0,
			left: 0,
			display: 'none',
			opacity: 0
		});
		
		/** Put everything together */
//		element.wrap('<div></div>'); //container
//			form.append(input);
//			element.after(form);
//			element.after(iframe);
		form.append(input);
		form.appendTo("body");
		iframe.appendTo("body");

	
		/** Find the container and make it nice and snug */
		var container = element.parent();
//		var container = element.parent().css({
//			position: 'relative',
//			height: element.outerHeight()+'px',
//			width: element.outerWidth()+'px',
//			overflow: 'hidden',
//			cursor: 'pointer',
//			margin: 0,
//			padding: 0
//		});
			
		/** Put our file input in the right place */
		// input.css('marginTop', -container.height()-10+'px');

		/** Move the input with the mouse to make sure it get clicked! */
		var input_t,input_l
		element.mousemove(function(e){
//			if(IE){
//				input_t=element.offset().top;
//				input_l=element.offset().left-150;
//			}else{
//				input_t=element.offset().top;
//				input_l=element.offset().left-8;
//			}
			if(IE){
				input_t=e.pageY;
				input_l=e.pageX;
				input_t=input_t-5;
				input_l=input_l-180;
			}else{
				input_t=e.pageY;
				input_l=e.pageX;
				input_l=input_l-8;
			}
			input.css({
				display: 'block',
				top: input_t+'px',//e.pageY-container.offset().top+'px',
				left: input_l+'px'//e.pageX-container.offset().left+'px'
			});
		});
		input.mouseout(function(e){
			input.css({
				display: 'none',
				top: 0,
				left: 0
			});
		});
		
		/** Watch for file selection */
		input.change(function() {
			/** Do something when a file is selected. */
			self.onSelect(); 
			
			/** Submit the form automaticly after selecting the file */
			if(self.autoSubmit) {
				self.submit();
			}
		});
		
		/** Methods */
		$.extend(this, {
			autoSubmit: true,
			onSubmit: options.onSubmit,
			onComplete: options.onComplete,
			onSelect: options.onSelect,
		
			/** get filename */		
			filename: function() {
				return input.attr('value');
			},
			
			/** get/set params */
			params: function(params) {
				var params = params ? params : false;
				
				if(params) {
					options.params = $.extend(options.params, params);
				}
				else {
					return options.params;
				}
			},
			
			/** get/set name */
			name: function(name) {
				var name = name ? name : false;
				
				if(name) {
					input.attr('name', value);
				}
				else {
					return input.attr('name');
				}
			},
			
			/** get/set action */
			action: function(action) {
				var action = action ? action : false;
				
				if(action) {
					form.attr('action', action);
				}
				else {
					return form.attr('action');
				}
			},
			
			/** get/set enctype */
			enctype: function(enctype) {
				var enctype = enctype ? enctype : false;
				
				if(enctype) {
					form.attr('enctype', enctype);
				}
				else {
					return form.attr('enctype');
				}
			},
			
			/** set options */
			set: function(obj, value) {
				var value =	value ? value : false;
								
				function option(action, value) {
					switch(action) {
						default:
							throw new Error('[jQuery.ocupload.set] \''+action+'\' is an invalid option.');
							break;
						case 'name':
							self.name(value);
							break;
						case 'action':
							self.action(value);
							break;
						case 'enctype':
							self.enctype(value);
							break;
						case 'params':
							self.params(value);
							break;
						case 'autoSubmit':
							self.autoSubmit = value;
							break;
						case 'onSubmit':
							self.onSubmit = value;
							break;
						case 'onComplete':
							self.onComplete = value;
							break;
						case 'onSelect':
							self.onSelect = value;
							break;
					}
				}				
				
				if(value) {
					option(obj, value);
				}
				else {				
					$.each(obj, function(key, value) {
						option(key, value);
					});
				}
			},
			
			/** Submit the form */
			submit: function() {
				/** Do something before we upload */
				this.onSubmit();
				
				/** add additional paramters before sending */
				$.each(options.params, function(key, value) {
					form.append($(
						'<input '+
							'type="hidden" '+
							'name="'+key+'" '+
							'value="'+value+'" '+
						'/>'
					));
				});
				
				/** Submit the actual form */
				form.submit(); 
				
				/** Do something after we are finished uploading */
				iframe.unbind().load(function() {
					/** Get a response from the server in plain text */
					var myFrame = document.getElementById(iframe.attr('name'));
					var response = $(myFrame.contentWindow.document.body).text();
					
					/** Do something on complete */
					self.onComplete(response,options.callBackOption); //done :D
				});
			}
		});
	}
})(jQuery);

(function ($){
	$.fn.tristate = function(options) {  
	
		var config = {
			checkedClass: 'checked',
			fullClass: 'full',
			heading: 'span.label',
			partialClass: 'partial',
			showOriginalInputs: false
		};
		var opts = $.extend(config,options);
		
		return this.each(function() {  
			obj = $(this);

			var triState = {
				init: function () {
					// Add proxy checkbox for each heading
					obj.find(opts.heading).before('<a href="#" class="checkbox">Heading</a>').wrap('<label></label>');
					
					var $inputs = obj.find('input[type="checkbox"]');
					for (var i=0, y=$inputs.length; i<y; i++) {
						// hide the input fields
						triState.hideInput($($inputs[i]));
					}
					
					// Initialize proxy links
					triState.updateProxyStates();
					
					// Set click behavior on appropriate input fields
					obj.find('a.checkbox, label').bind('click', function (e) {
						e.preventDefault();
						triState.initProxyClick($(this));
					});
				},
				
				checkSiblings: function ($el, bChecked) {
					var $siblings = $el.parent('li').siblings().children('a.checkbox');
					var iCheckCount = 0;
					var $parentList = $el.parent('li').parent('ul');
					var $descendantLists = $parentList.find('ul');
					var sResult;
					
					if (bChecked) { iCheckCount++; }
					
					// Loop through the list of siblings to see if any are checked
					for (var i=0; i<$siblings.length; i++) {
						if ($($siblings[i]).hasClass(opts.checkedClass) || $($siblings[i]).hasClass(opts.partialClass)) { iCheckCount++; }
					}
					
					// Depending on how many siblings are checked set the appropriate result string
					(iCheckCount < $siblings.length+1) ? sResult = opts.partialClass : sResult = opts.fullClass;
					for (var x=0; x<$descendantLists.length; x++) {
						if ($($descendantLists[x]).hasClass(opts.partialClass)) { sResult = opts.partialClass; }
					}
					if (iCheckCount === 0) { sResult = ''; }
					
					// Set the appropriate class to the parent list
					$parentList.removeClass(opts.partialClass + ' ' + opts.fullClass).addClass(sResult);
				},
				
				initProxyClick: function ($el) {
					var bChecked;
					triState.setInputField($el);
					
					// Initialize the input field
					($($el).hasClass(opts.checkedClass) === true || $($el).hasClass(opts.partialClass) === true) ? bChecked = true : bChecked = false ;
											
					// Check for 'checked' siblings
					triState.checkSiblings($el, bChecked);
					
					// Check/uncheck this elements associated label
					triState.setLabel($el, bChecked);
					
					// Check/uncheck this elements descendants appropriately
					triState.setDescendants($el, bChecked);
					
					// Check/uncheck this element's ancestors appropriately
					triState.setAncestors($el, bChecked);
					
					triState.updateProxyStates();
				},
				
				updateProxyStates: function () {
					var $inputs = $('input:checked', obj);
					var $proxyLink;
					for (var i=0, y=$inputs.length; i<y; i++) {
						$proxyLink = $($inputs[i]).siblings('a.checkbox');
						triState.setInputField($proxyLink, 'checked');
						triState.checkSiblings($($inputs[i]), true);
						triState.setAncestors($($inputs[i]), true);
					}
				},
				
				hideInput: function ($el) {
					var sName = $el.attr('name');
					var $newCheckbox = $('<a href="#" class="checkbox">'+sName+'</a>');
					$newCheckbox.data("name", sName);
					$el.before($newCheckbox);
					if (!opts.showOriginalInputs) { $el.hide(); }
				},
				
				setAncestors: function ($el, bChecked) {
					var bAncestorCheck;
					var $parentList = $el.parent('li').parent('ul');
					var $ancestorInput = $parentList.parent('li').children('a.checkbox');
					
					// Check the class on the parentList and check the ancestorInput appropriately
					if (!$parentList.hasClass(opts.partialClass) && !$parentList.hasClass(opts.fullClass)) {
						$ancestorInput.removeClass(opts.checkedClass +' '+ opts.partialClass);
						triState.setInputField($ancestorInput, 'unchecked');
						triState.setLabel($ancestorInput, bChecked);
						bAncestorCheck = false;
					} else {
						if(!$parentList.hasClass(opts.partialClass)){
							triState.setInputField($ancestorInput, 'checked');
						}
						triState.setLabel($ancestorInput, bChecked);
						if ($parentList.hasClass(opts.partialClass)) { $ancestorInput.removeClass(opts.checkedClass).addClass(opts.partialClass); }
						if ($parentList.hasClass(opts.fullClass)) { $ancestorInput.removeClass(opts.partialClass).addClass(opts.checkedClass); }
						bAncestorCheck = true;
					}
					
					// As long as we're not at the root element then keep traversing up the chain to check each input appropriately
					if ($ancestorInput.length > 0) {
						triState.checkSiblings($ancestorInput, bAncestorCheck);
						triState.setAncestors($ancestorInput, $ancestorInput.hasClass(opts.checkedClass));
					}
				},
				
				setDescendants: function ($el, bChecked) {
					var $parentItem = $el.parent('li');
					var $descendantList = $parentItem.find('ul');
					var $descendantInputs = $parentItem.find('a.checkbox');
					
					if (bChecked) {
						for (var i=0, j=$descendantInputs.length; i<j; i++) {
							triState.setInputField($($descendantInputs[i]), 'checked');
							triState.setLabel($($descendantInputs[i]), bChecked);
						}
						$descendantList.removeClass(opts.partialClass + ' ' + opts.fullClass).addClass(opts.fullClass);
					} else {
						for (var g=0, y=$descendantInputs.length; g<y; g++) {
							triState.setInputField($($descendantInputs[g]), 'unchecked');
							triState.setLabel($($descendantInputs[g]), bChecked);
						}
						$descendantList.removeClass(opts.partialClass + ' ' + opts.fullClass);
					}
				},
				
				setInputField: function ($el, state) {
					var sState = state || '';
					switch (sState) {
						case '':
							($el.hasClass(opts.checkedClass) === true) ? triState.uncheckInput($el) : triState.checkInput($el);
							break;
						case 'checked':
							triState.checkInput($el);
							break;
						case 'unchecked':
							triState.uncheckInput($el);
							break;
					}
				},
				
				setLabel: function ($el, state) {
					$label = $el.siblings('label');
					(state) ? triState.checkLabel($label) : triState.uncheckLabel($label);
				},
				
				checkInput: function ($el) {
					$el.removeClass(opts.partialClass).addClass(opts.checkedClass).siblings('input[name="'+$el.data('name')+'"]').attr('checked', 'checked');
				},
				
				uncheckInput: function ($el) {
					$el.removeClass(opts.checkedClass +' '+ opts.partialClass).siblings('input[name="'+$el.data('name')+'"]').attr('checked', '');
				},
				
				checkLabel: function ($el) {
					$el.removeClass(opts.partialClass).addClass(opts.checkedClass);
				},
				
				uncheckLabel: function ($el) {
					$el.removeClass(opts.checkedClass +' '+ opts.partialClass);
				}
			};
			
			triState.init();			

		});  
	};
})(jQuery);





